package com.abuaziz.librarys.tset_adapter

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.abuaziz.librarys.R
import com.pawegio.kandroid.textWatcher
import com.ug_project.adapters.RecyclerAdapter
import com.ug_project.adapters.createRecyclerAdapter
import com.ug_project.views.click
import com.ug_project.views.content
import com.ug_project.views.findTextView
import com.yurqi.dialog.SDialog
import kotlinx.android.synthetic.main.activity_recycler_view.*
import me.ezitku.base.dialog.createBottomDialog

class RecyclerAdapter : AppCompatActivity() {


    data class Person(var name: String)

    var datas = ArrayList<Person>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view)

        recyclerView.layoutManager = LinearLayoutManager(this)

        recyclerView.adapter = createRecyclerAdapter(android.R.layout.simple_spinner_item, datas) {
            OnBindViewHolderIData { view, itemViewResID, data, position ->
                view.findTextView(android.R.id.text1).content = data.name
            }
        }

        add_data.click {
            initDatas()
        }


        search.textWatcher {
            onTextChanged { input, i, i2, i3 ->
                var new = input?.toString() ?: ""
                if (new.isNotEmpty()) {
                    (recyclerView.adapter as RecyclerAdapter<Person>).filter {
                        it.name.contains(new)
                    }
                } else {
                    (recyclerView.adapter as RecyclerAdapter<Person>).filter { true }
                }
            }
        }

        initDatas()




    }

    fun initDatas() {
        for (i in 0..10) {
            datas.add(Person("Item $i"))
        }
        (recyclerView.adapter as RecyclerAdapter<Person>).refresh()
    }
}